package br.com.hstar.client.config;

import br.com.hstar.client.ServiceFactory;
import br.com.hstar.client.util.FormPanel;
import br.com.hstar.client.util.HstarDialogBox;
import br.com.hstar.client.util.HstarUserException;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class CadQuartoEditPanel extends FormPanel {

	private Button incluir;
	private Button cancelar;
	private Button excluir;
	
	private TextBox numero;
	private CadQuartoUI cadQuartoUI;

	public CadQuartoEditPanel(String _title) {
		super(_title);
		cadQuartoUI = new CadQuartoUI();
		init(true);
	}
	
	public CadQuartoEditPanel(String _title, Long id) {
		super(_title);
		cadQuartoUI = new CadQuartoUI();
		cadQuartoUI.setId(id);
		init(false);
		ServiceFactory.createCadQuartoService().retrieveById(id, new RetrieveAsyncCallback());
	}
	
	
	
	//Methods -------------------------------------------------------------------
	
	private void init(boolean isNew) {

		numero = addTextBox("Número", 2, 4, "");		
		
		if(isNew) {
			incluir  = addButton("Incluir");
			cancelar = addButton("Cancelar");
		} else {
			incluir  = addButton("Alterar");
			cancelar = addButton("Cancelar");
			excluir = addButton("Excluir");
			excluir.addClickListener(new ClickListener() {
				public void onClick(Widget sender) {
					ServiceFactory.createCadQuartoService().delete(cadQuartoUI.getId(), new DeleteAsyncCallback());
				}
			});
		}
		
		incluir.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				 try {
					copyData();
					ServiceFactory.createCadQuartoService().saveOrUpdate(cadQuartoUI, new SaveAsyncCallback());
				} catch (HstarUserException e) {					
					new HstarDialogBox("Quarto",e.getMessage());
				}
				
			}
		});
		cancelar.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				RootPanel.get("boxContent").clear();
				RootPanel.get("boxContent").add(new CadQuartoListPanel("Quartos"));
			}
		});
		
	}

	private void copyData() throws HstarUserException {

		String msgErro = "";

		if (numero.getText().trim() == "") {
			msgErro += " - Digite o número do quarto <br>";
		}

		if (msgErro != "") {
			throw new HstarUserException(msgErro);
		}

		cadQuartoUI.setNumero(new Integer(numero.getText()));

	}

	// INNER CLASSES
	// ----------------------------------------------------------------
	private class SaveAsyncCallback implements AsyncCallback {
		public void onFailure(Throwable caught) {
			if (caught instanceof HstarUserException) {
				new HstarDialogBox("Quarto", caught.getMessage());
			}
		}

		public void onSuccess(Object result) {
			RootPanel.get("boxContent").clear();
			RootPanel.get("boxContent").add(new CadQuartoListPanel("Quartos"));
		}
	}
	
	private class DeleteAsyncCallback implements AsyncCallback {
		public void onFailure(Throwable caught) {
			Window.alert("ERRO - deletar quarto");
		}

		public void onSuccess(Object result) {
			RootPanel.get("boxContent").clear();
			RootPanel.get("boxContent").add(new CadQuartoListPanel("Quartos"));
		}
	}
	
	//carrega os campos. Usado para quando está editando
	private class RetrieveAsyncCallback implements AsyncCallback {
		public void onFailure(Throwable caught) {
			Window.alert("ERRO - Cheque edit panel");
		}

		public void onSuccess(Object result) {
			CadQuartoUI qUI = (CadQuartoUI)result;
			if(qUI != null) {
				numero.setText(qUI.getNumero().toString());
			}
		}
	}
}      